package com.droowa.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.droowa.domain.BoardList;
import com.droowa.domain.BoardManagement;
import com.droowa.domain.BoardSort;
import com.droowa.domain.CompletePC;
import com.droowa.domain.CompletePCResult;
import com.droowa.model.service.BoardManagementService;
import com.droowa.model.service.ProductManagementService;

@Controller
@RequestMapping("/page/")
public class PageController {
	@Autowired
	ProductManagementService pms;
	@Autowired
	BoardManagementService bms;
	
	@RequestMapping("manager.do")
	public String managerMode(){
		return "admin.main";
	}
	
	@RequestMapping("customer.do")
	public String customerMode(HttpServletRequest request) throws SQLException{
		List list=pms.getCompletePCList();
		request.setAttribute("list", list);
		List<CompletePCResult> firstList= new ArrayList<CompletePCResult>();
		List<CompletePCResult> secondList= new ArrayList<CompletePCResult>();
		List<CompletePCResult> resultList= new ArrayList<CompletePCResult>();
		
		resultList = pms.getCompletePCByPrice(10, 300000);//매개변수 1 : StartPrice, 2: EndPrice
		for(int idx=0; idx<2; idx++){
			firstList.add((CompletePCResult) resultList.get(idx));
		}
			
		resultList = pms.getCompletePCByPrice(300001, 500000);//매개변수 1 : StartPrice, 2: EndPrice
		for(int idx=0; idx<2; idx++){
			firstList.add((CompletePCResult) resultList.get(idx));
		}
		request.setAttribute("firstList", firstList);
		
		resultList = pms.getCompletePCByPrice(500001, 1000000000);//매개변수 1 : StartPrice, 2: EndPrice
		for(int idx=0; idx<2; idx++){
			secondList.add((CompletePCResult) resultList.get(idx));
		}
		
		resultList = pms.getCompletePCByCPUBrand("cpu", "intel");
		for(int idx=0; idx<1; idx++){
			secondList.add((CompletePCResult) resultList.get(idx));
		}
		resultList = pms.getCompletePCByCPUBrand("cpu", "amd");
		for(int idx=0; idx<1; idx++){
			secondList.add((CompletePCResult) resultList.get(idx));
		}
		request.setAttribute("secondList", secondList);
		
		//게시판 메뉴 불러오기 및 mapper applicationParam 추가
		//회원 비회원 가리지 않고 메뉴를 계속해서 보여줘야하기 때문에 application 객체로 저장시켜야한다.
		List<BoardSort> boardList = bms.searchOpenList();
		//ServletContext에 객체 공유
		ServletContext context = request.getSession().getServletContext();
		context.setAttribute("boardList", boardList);
	 
		return "customer.main";
	}
	@RequestMapping("mypage.do")
	public String myPage(HttpServletRequest request){
		return "customer.myPage_form";
	}
}
